package ru.vpitervip.app.client.ui;

import java.util.Date;

import ru.vpitervip.app.client.App;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.i18n.client.DateTimeFormat.PredefinedFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.datepicker.client.DateBox;
import com.google.gwt.user.datepicker.client.DateBox.Format;

public class BookingView extends Composite {

	private static BookingViewUiBinder uiBinder = GWT
			.create(BookingViewUiBinder.class);
	@UiField Anchor backToFlatListLink;
	@UiField TextBox nameField;
	@UiField TextBox emailField;
	@UiField TextBox phoneField;
	@UiField ListBox peopleNumList;
	@UiField TextArea messageField;
	@UiField Button sendButton;
	@UiField DateBox inDateField;
	@UiField DateBox outDateField;
	
	@UiField SimplePanel contentPanel;
	
	@UiField AbsolutePanel formPanel;
	
	@UiField Image loadingImage;
	
	private final String emailPattern="^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
	
	
	boolean standAlone;
	
	int flatId;
	
	private Presenter listener;

	interface BookingViewUiBinder extends UiBinder<Widget, BookingView> {
	}
	
	
	public interface Presenter{
		
		void backToFlatList();

		void sendButtonClicked();
		
	}
	

	public BookingView() {
		initWidget(uiBinder.createAndBindUi(this));
		init();
	}

	private void init() {
		
		for (int i=1;i<12;i++) peopleNumList.addItem(""+i);
		peopleNumList.setSelectedIndex(1);
		
		backToFlatListLink.addClickHandler(new ClickHandler(){

			@Override
			public void onClick(ClickEvent event) {
			if (listener!=null)	listener.backToFlatList();
			}
			
		});
		
		inDateField.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT)));
		outDateField.setFormat(new DateBox.DefaultFormat(DateTimeFormat.getFormat(PredefinedFormat.DATE_TIME_SHORT)));
		
		inDateField.getDatePicker().addStyleName(App.getAppResources().style().dateStyle());
		outDateField.getDatePicker().addStyleName(App.getAppResources().style().dateStyle());
		
	}
	
	public void setPresenter(Presenter listener){
		
	
		this.listener=listener;
		
	}

	public void setStandAlone(boolean standAlone) {
		this.standAlone=standAlone;
		backToFlatListLink.setVisible(!standAlone);
	}

	public void setFlatId(int flatId) {
		this.flatId=flatId;
		
	}
	
	@UiHandler("sendButton")
	public void sendButtonClicked(ClickEvent event){
		
	    boolean valid=true;
		
		if (nameField.getValue()==null||nameField.getValue().equals("")) {
			nameField.addStyleName("dateBoxFormatError");
			valid=false;
		}else{
			nameField.removeStyleName("dateBoxFormatError");
		}
		
		if (emailField.getValue()==null||!emailField.getValue().matches(emailPattern)) {
			emailField.addStyleName("dateBoxFormatError");
			valid=false;
		}else{
			emailField.removeStyleName("dateBoxFormatError");
		}
		
		
		
		if (valid) {
			sendButton.setEnabled(false);
			listener.sendButtonClicked();
			loadingImage.setVisible(true);
		}
		
	}
	
	public void setFormView(){
		contentPanel.setWidget(formPanel);
		loadingImage.setVisible(false);
		sendButton.setEnabled(true);
	}
	
	public void setCommittedView(){
		contentPanel.setWidget(new HTML("<br><br><br><br><br><br><br><br><br><center>Заявка отправлена</center>"));
	}
	
	
	public String getName(){
		return nameField.getValue();
	}
	    
	public String getTel(){
		return phoneField.getValue();
	}
	    
	public String getEmail(){
		return emailField.getValue();
	}
	    
	public int getObjectId(){
		return flatId;
	}
	    
	public int getPeopleNum(){
		return peopleNumList.getSelectedIndex()+1;
	}
	    
	public Date getInDate(){
		return inDateField.getValue();
	}
	    
	public Date getOutDate(){
		return outDateField.getValue();
	}
	    
	public String getNote(){
		return messageField.getValue();
	}
	
	

}
